<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Material;

use App\Controller\Admin\AdminController;
use App\Model\MaterialAssign;
use App\Model\MaterialFlow;
use App\Model\MaterialSheetAssign;
use App\Repositories\AreaRepository;
use App\Repositories\CommunityRepository;
use App\Repositories\MaterialAssignRepository;
use App\Repositories\MaterialFlowRepository;
use App\Repositories\MaterialProjectRepository;
use App\Repositories\MaterialProjectSheetRepository;
use App\Repositories\MaterialReportRepository;
use App\Repositories\MaterialRepository;
use App\Repositories\MaterialSheetAssignRepository;
use App\Repositories\StreetRepository;
use App\Request\StoreMaterialRequest;
use App\Services\AdminService;
use App\Services\MaterialService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class ReportController extends AdminController
{
    /**
     * @Inject
     * @var AdminService
     */
    public $admin_service;
    /**
     * @Inject
     * @var MaterialService
     */
    public $material_service;


    public function sheetList(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $material_id = $this->request->input('material_id',0);
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $title = $this->request->input('title','');
//        $type=3;
//        $data_id = 6;
//        if($admin->district_id){
//            $type=MaterialAssign::TYPE_ONE;
//            $data_id = $admin->district_id;
//        }
//        if($admin->street_id){
//            $type = MaterialAssign::TYPE_TWO;
//            $data_id = $admin->street_id;
//        }
//        if($admin->community_id){
//            $type = MaterialAssign::TYPE_THREE;
//            $data_id = $admin->community_id;
//        }
        $columns=[
            'material_sheet_assign.*',
            'sheet_name',
            'sheet_description'
        ];
        $search=[
            'type' =>[
                'field' => 'type',
                'value' => $self_admin['type']
            ],
            'data_id' => [
                'field' => 'data_id',
                'value' => $self_admin['data_id']
            ]
        ];
        if($material_id){
            $search['material_id']=[
                'field' => 'material_sheet_assign.material_id',
                'value' => $material_id
            ];
        }
        $join = [
            'material_project_sheet' => [
                'on' => ['material_project_sheet.id','=','material_sheet_assign.sheet_id'],
                'where'=>[]
            ]
        ];

        if($title!=''){
            $search['sheet_name'] = [
                'field' => 'sheet_name',
                'value' => $title,
                'method' => 'like'
            ];
        }

        $orderBy= [
            [
                'field' => 'material_sheet_assign.id',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialSheetAssignRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            $project_ids =[];
            $material_ids=[];
            foreach ($result['result'] as &$item){
                if(isset($project_ids[$item['project_id']])){
                    $item['project'] = $project_ids[$item['project_id']];
                }else{
                    $project = MaterialProjectRepository::getInstance()->one(['id','name'],[[
                        'field' => 'id',
                        'value' => $item['project_id']
                    ]]);
                    $project_ids[$item['project_id']] = $project;
                    $item['project'] = $project;
                }
                if(isset($material_ids[$item['material_id']])){
                    $item['material'] = $material_ids[$item['material_id']];
                }else{
                    $material = MaterialRepository::getInstance()->one(['id','title','start_time','end_time'],[[
                        'field' => 'id',
                        'value' => $item['material_id']
                    ]]);
                    $material_ids[$item['material_id']] = $material;
                    $item['material'] = $material;
                }
            }
        }
        //$result = MaterialAssignRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        return $this->response->success($result);
    }

    /**
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function list(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $title = $this->request->input('title','');
//        $type=3;
//        $data_id = 6;
//        if($admin->district_id){
//            $type=MaterialAssign::TYPE_ONE;
//            $data_id = $admin->district_id;
//        }
//        if($admin->street_id){
//            $type = MaterialAssign::TYPE_TWO;
//            $data_id = $admin->street_id;
//        }
//        if($admin->community_id){
//            $type = MaterialAssign::TYPE_THREE;
//            $data_id = $admin->community_id;
//        }
        $columns=[
            'material.id',
            'material_assign.status',
            'material_assign.material_id',
            'material.title',
            'start_time',
            'end_time',
            'material_assign.created_at'
        ];
        $search=[
            'type' =>[
                'field' => 'type',
                'value' => $self_admin['type']
            ],
            'data_id' => [
                'field' => 'data_id',
                'value' => $self_admin['data_id']
            ]
        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_assign.material_id'],
                'where'=>[]
            ]
        ];
        if($title!=''){
            $search['title'] = [
                'field' => 'title',
                'value' => $title,
                'method' => 'like'
            ];
        }

        $orderBy= [
            [
                'field' => 'material_assign.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialAssignRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            foreach ($result['result'] as &$item){
               $sheet_count = MaterialSheetAssign::query()->where('material_id',$item['id'])
                   ->where('type',$self_admin['type'])
                   ->where('data_id',$self_admin['data_id'])->count('id');
               $sheet_upload_count = MaterialSheetAssign::query()->where('material_id',$item['id'])
                    ->where('type',$self_admin['type'])
                    ->where('data_id',$self_admin['data_id'])
                   ->where('status','!=',MaterialSheetAssign::STATUS_DEFAULT)->count('id');
                $sheet_fail_count = MaterialSheetAssign::query()->where('material_id',$item['id'])
                    ->where('type',$self_admin['type'])
                    ->where('data_id',$self_admin['data_id'])
                    ->where('status',MaterialSheetAssign::STATUS_VERIFYFAIL)->count('id');
               $item['sheet_count'] = $sheet_count;
               $item['sheet_upload_count'] = $sheet_upload_count;
               $item['sheet_fail_count'] = $sheet_fail_count;
            }
        }
        return $this->response->success($result);
    }

    public function submit(){
        $admin = $this->request->getAttribute('admin');
        $data = $this->request->all();
        $data['submit_data'] = json_decode($data['submit_data'],true);
        if(!is_array($data['submit_data'])){
            return $this->response->error(10040,'上传格式不正确');
        }
        $result = $this->material_service->reportSheet($data,$admin);
        if(!$result['success']){
            return $this->response->error(10050,$result['msg']);
        }
        return $this->response->success($result);
    }

    public function submitVerify(){
        $admin = $this->request->getAttribute('admin');
        $assign_id = $this->request->input('material_assign_id',0);
        $result = $this->material_service->submitVerifySheet($assign_id,$admin);
        if(!$result['success']){
            return $this->response->error(10050,$result['msg']);
        }
        return $this->response->success($result);
    }

    public function detail(){
        $assign_id = $this->request->input('id',0);
        $assign = MaterialSheetAssignRepository::getInstance()->getById($assign_id);
        if(!$assign){
            return $this->response->error(1404,'数据不存在');
        }
        $material = MaterialRepository::getInstance()->getById($assign->material_id);
        $project = MaterialProjectRepository::getInstance()->getById($assign->project_id);
        $sheet = MaterialProjectSheetRepository::getInstance()->getById($assign->sheet_id);

        $result = MaterialReportRepository::getInstance()->all('*',[[
            'field' => 'material_assign_id',
            'value' => $assign_id
        ]]);
        $assign->reports = $result;
        $flow = MaterialFlowRepository::getInstance()->one(['id','mask','status'],[[
            'field' => 'material_assign_id',
            'value' => $assign_id
        ]],[[
            'field' => 'id',
            'sort' => 'desc'
        ]]);
        $assign->flow = $flow?$flow:new \stdClass();
        return $this->response->success([
            'material' => $material,
            'assign' => $assign,
            'project' => $project,
            'sheet' => $sheet
        ]);
    }

    public function detailbak(){
        $assign_id = $this->request->input('id',0);
        $assign = MaterialAssignRepository::getInstance()->getById($assign_id);
        if(!$assign){
            return $this->response->error(1404,'数据不存在');
        }
        $material = MaterialRepository::getInstance()->getById($assign->material_id);
        $projects = MaterialProjectRepository::getInstance()->all('*',[[
            'field' => 'material_id',
            'value' => $assign->material_id
        ]],[[
            'field' => 'listorder',
            'sort' => 'asc'
        ]]);
        foreach ($projects as &$project){
            $project->sheets = MaterialProjectSheetRepository::getInstance()->all('*',[[
                'field' => 'project_id',
                'value' => $project->id
            ]],[[
                'field' => 'listorder',
                'sort' => 'asc'
            ]]);
        }
        $result = MaterialReportRepository::getInstance()->all('*',[[
            'field' => 'material_assign_id',
            'value' => $assign_id
        ]]);
        if($projects->count()){
            foreach ($projects as &$item){
                foreach ($item->sheets as &$sheet){
                    $reports=collect($result)->filter(function($rep) use($sheet){
                        return $rep->material_sheet_id==$sheet->id;
                    });
                    if($reports->count()){
                        $sheet->reports = array_merge($reports->toArray(),[]);
                    }else{
                        $sheet->reports = [];
                    }
                }

            }
        }

        return $this->response->success([
            'material' => $material,
            'assign' => $assign,
            'projects' => $projects
        ]);
    }

    public function verifyListbak(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'to_data_type' =>[
                'field' => 'to_data_type',
                'value' => $self_admin['type']
            ],
            'to_data_id' => [
                'field' => 'to_data_id',
                'value' => $self_admin['data_id']
            ],
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_ONE
            ]
        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ]
        ];

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $item['report_from'] = MaterialAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
            }
        }
        return $this->response->success($result);
    }

    public function verifyList(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $status = $this->request->input('status','all');
        $title = $this->request->input('title','');
        $area_ids = $this->request->input('area_ids','');
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $district_id = $street_id = $community_id=0;
        if($self_admin['type']==MaterialAssign::TYPE_THREE){
            return $this->response->error(10020,'您不具备审核权限');
        }
        if($self_admin['type']==MaterialAssign::TYPE_TWO){
            if($area_ids!=''){
                $area_ids = explode(',',$area_ids);
                if(count($area_ids)==1){
                    $community_id = $area_ids[0];
                }else{
                    $community_id = $area_ids[count($area_ids)-1];
                }
            }
        }
        if($self_admin['type']==MaterialAssign::TYPE_ONE){
            if($area_ids!=''){
                $area_ids = explode(',',$area_ids);
                if(count($area_ids)==1){
                    $street_id = $area_ids[0];
                }else{
                    $street_id = $area_ids[0];
                    $community_id = $area_ids[1];
                }
            }
        }
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'to_data_type' =>[
                'field' => 'to_data_type',
                'value' => $self_admin['type']
            ],
            'to_data_id' => [
                'field' => 'to_data_id',
                'value' => $self_admin['data_id']
            ],
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_ONE
            ]
        ];
        if($status!='all'){
            $search['status']=[
                'field' => 'material_flow.status',
                'value' =>$status
            ];
        }
//        $join = [
//            'material' => [
//                'on' => ['material.id','=','material_flow.material_id'],
//                'where'=>[]
//            ]
//        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ],
            'material_sheet_assign' => [
                'on' => ['material_sheet_assign.id','=','material_flow.material_assign_id'],
                'where'=>[]
            ]
        ];
        if($title!=''){
            $search['title'] =[
                'field' => 'title',
                'value' => $title,
                'method' => 'like'
            ];
        }

        if($district_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $district_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_ONE
            ];
        }
        if($street_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $street_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_TWO
            ];
        }
        if($community_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $community_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_THREE
            ];
        }

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join,'',['material_flow.material_id','assign_parent_id']);
        if(!empty($result['result'])){
            //$project_ids =[];
            $material_ids=[];
            //$sheet_ids = [];
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $assign_from= MaterialSheetAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
                $assign = $assign_from['assign'];
                $item['report_from'] = $assign_from['from'];
                $item['contact'] = $assign_from['contact'];
                $item['sheet_count'] = MaterialFlow::query()->where('material_id',$item['material_id'])
                    ->where('flow_type',MaterialFlow::FLOW_TYPE_ONE)
                    ->where('to_data_type',$self_admin['type'])
                    ->where('to_data_id',$self_admin['data_id'])
                    ->where('assign_parent_id',$item['assign_parent_id'])->count('id');
                $item['sheet_verify_count'] = MaterialFlow::query()->where('material_id',$item['material_id'])->where('is_end',1)
                    ->where('flow_type',MaterialFlow::FLOW_TYPE_ONE)
                    ->where('to_data_type',$self_admin['type'])
                    ->where('to_data_id',$self_admin['data_id'])
                    ->where('assign_parent_id',$item['assign_parent_id'])->count('id');
//                if(isset($project_ids[$assign->project_id])){
//                    $item['project'] = $project_ids[$assign->project_id];
//                }else{
//                    $project = MaterialProjectRepository::getInstance()->one(['id','name'],[[
//                        'field' => 'id',
//                        'value' => $assign->project_id
//                    ]]);
//                    $project_ids[$assign->project_id] = $project;
//                    $item['project'] = $project;
//                }
                if(isset($material_ids[$assign->material_id])){
                    $item['material'] = $material_ids[$assign->material_id];
                }else{
                    $material = MaterialRepository::getInstance()->one(['id','title','start_time','end_time'],[[
                        'field' => 'id',
                        'value' => $assign->material_id
                    ]]);
                    $material_ids[$assign->material_id] = $material;
                    $item['material'] = $material;
                }
//
//                if(isset($sheet_ids[$assign->sheet_id])){
//                    $item['sheet'] = $sheet_ids[$assign->sheet_id];
//                }else{
//                    $sheet = MaterialProjectSheetRepository::getInstance()->one(['id','sheet_name'],[[
//                        'field' => 'id',
//                        'value' => $assign->sheet_id
//                    ]]);
//                    $sheet_ids[$assign->sheet_id] = $sheet;
//                    $item['sheet'] = $sheet;
//                }

            }
        }
        return $this->response->success($result);
    }

    public function verifySheetList(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $material_id = $this->request->input('material_id',0);
        $assign_parent_id = $this->request->input('assign_parent_id',0);
        $status = $this->request->input('status','all');
        $title = $this->request->input('title','');
        $area_ids = $this->request->input('area_ids','');
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $district_id = $street_id = $community_id=0;
        if($self_admin['type']==MaterialAssign::TYPE_THREE){
            return $this->response->error(10020,'您不具备审核权限');
        }
        if($self_admin['type']==MaterialAssign::TYPE_TWO){
            if($area_ids!=''){
                $area_ids = explode(',',$area_ids);
                if(count($area_ids)==1){
                    $community_id = $area_ids[0];
                }else{
                    $community_id = $area_ids[count($area_ids)-1];
                }
            }
        }
        if($self_admin['type']==MaterialAssign::TYPE_ONE){
            if($area_ids!=''){
                $area_ids = explode(',',$area_ids);
                if(count($area_ids)==1){
                    $street_id = $area_ids[0];
                }else{
                    $street_id = $area_ids[0];
                    $community_id = $area_ids[1];
                }
            }
        }
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'to_data_type' =>[
                'field' => 'to_data_type',
                'value' => $self_admin['type']
            ],
            'to_data_id' => [
                'field' => 'to_data_id',
                'value' => $self_admin['data_id']
            ],
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_ONE
            ]
        ];
        if($status!='all'){
            $search['status']=[
                'field' => 'material_flow.status',
                'value' =>$status
            ];
        }
//        $join = [
//            'material' => [
//                'on' => ['material.id','=','material_flow.material_id'],
//                'where'=>[]
//            ]
//        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ],
            'material_sheet_assign' => [
                'on' => ['material_sheet_assign.id','=','material_flow.material_assign_id'],
                'where'=>[]
            ]
        ];
        if($title!=''){
            $search['title'] =[
                'field' => 'title',
                'value' => $title,
                'method' => 'like'
            ];
        }

        if($district_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $district_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_ONE
            ];
        }
        if($street_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $street_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_TWO
            ];
        }
        if($community_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $community_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_THREE
            ];
        }
        if($material_id){
            $search['material_id']=[
                'field' => 'material_flow.material_id',
                'value' => $material_id
            ];
        }
        if($assign_parent_id){
            $search['assign_parent_id'] = [
                'field' => 'material_flow.assign_parent_id',
                'value' => $assign_parent_id
            ];
        }

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            $project_ids =[];
            $material_ids=[];
            $sheet_ids = [];
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $assign_from= MaterialSheetAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
                $assign = $assign_from['assign'];
                $item['report_from'] = $assign_from['from'];
                $item['contact'] = $assign_from['contact'];
                if(isset($project_ids[$assign->project_id])){
                    $item['project'] = $project_ids[$assign->project_id];
                }else{
                    $project = MaterialProjectRepository::getInstance()->one(['id','name'],[[
                        'field' => 'id',
                        'value' => $assign->project_id
                    ]]);
                    $project_ids[$assign->project_id] = $project;
                    $item['project'] = $project;
                }
                if(isset($material_ids[$assign->material_id])){
                    $item['material'] = $material_ids[$assign->material_id];
                }else{
                    $material = MaterialRepository::getInstance()->one(['id','title','start_time','end_time'],[[
                        'field' => 'id',
                        'value' => $assign->material_id
                    ]]);
                    $material_ids[$assign->material_id] = $material;
                    $item['material'] = $material;
                }

                if(isset($sheet_ids[$assign->sheet_id])){
                    $item['sheet'] = $sheet_ids[$assign->sheet_id];
                }else{
                    $sheet = MaterialProjectSheetRepository::getInstance()->one(['id','sheet_name'],[[
                        'field' => 'id',
                        'value' => $assign->sheet_id
                    ]]);
                    $sheet_ids[$assign->sheet_id] = $sheet;
                    $item['sheet'] = $sheet;
                }

            }
        }
        return $this->response->success($result);
    }

    public function scoreList(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $status = $this->request->input('status','all');
        $title = $this->request->input('title','');
        $district_id = $this->request->input('district_id',0);
        $street_id = $this->request->input('street_id',0);
        $community_id = $this->request->input('community_id',0);
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_TWO
            ]
        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ],
            'material_sheet_assign' => [
                'on' => ['material_sheet_assign.id','=','material_flow.material_assign_id'],
                'where'=>[]
            ]
        ];

        if($status!='all'){
            $search['status']=[
                'field' => 'material_flow.status',
                'value' =>$status
            ];
        }
        if($title!=''){
            $search['title'] =[
                'field' => 'title',
                'value' => $title,
                'method' => 'like'
            ];
        }
        if($district_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $district_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_ONE
            ];
        }
        if($street_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $street_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_TWO
            ];
        }
        if($community_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $community_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_THREE
            ];
        }

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join,'',['material_flow.material_id','assign_parent_id']);
        if(!empty($result['result'])){
            //$project_ids =[];
            $material_ids=[];
            //$sheet_ids = [];
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $assign_from= MaterialSheetAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
                $assign = $assign_from['assign'];
                $item['assign'] = $assign_from['assign'];
                $item['report_from'] = $assign_from['from'];
                $item['contact'] = $assign_from['contact'];
                $item['sheet_count'] = MaterialFlow::query()->where('material_id',$item['material_id'])
                    ->where('to_data_type',$self_admin['type'])
                    ->where('to_data_id',$self_admin['data_id'])
                    ->where('assign_parent_id',$item['assign_parent_id'])->count('id');
                $item['sheet_score_count'] = MaterialFlow::query()->where('material_id',$item['material_id'])->where('is_end',1)
                    ->where('to_data_type',$self_admin['type'])
                    ->where('to_data_id',$self_admin['data_id'])
                    ->where('assign_parent_id',$item['assign_parent_id'])->count('id');
//                if(isset($project_ids[$assign->project_id])){
//                    $item['project'] = $project_ids[$assign->project_id];
//                }else{
//                    $project = MaterialProjectRepository::getInstance()->one(['id','name'],[[
//                        'field' => 'id',
//                        'value' => $assign->project_id
//                    ]]);
//                    $project_ids[$assign->project_id] = $project;
//                    $item['project'] = $project;
//                }
                if(isset($material_ids[$assign->material_id])){
                    $item['material'] = $material_ids[$assign->material_id];
                }else{
                    $material = MaterialRepository::getInstance()->one(['id','title','start_time','end_time'],[[
                        'field' => 'id',
                        'value' => $assign->material_id
                    ]]);
                    $material_ids[$assign->material_id] = $material;
                    $item['material'] = $material;
                }

//                if(isset($sheet_ids[$assign->sheet_id])){
//                    $item['sheet'] = $sheet_ids[$assign->sheet_id];
//                }else{
//                    $sheet = MaterialProjectSheetRepository::getInstance()->one(['id','sheet_name'],[[
//                        'field' => 'id',
//                        'value' => $assign->sheet_id
//                    ]]);
//                    $sheet_ids[$assign->sheet_id] = $sheet;
//                    $item['sheet'] = $sheet;
//                }

            }
        }
        return $this->response->success($result);
    }

    public function scoreSheetList(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $status = $this->request->input('status','all');
        $material_id = $this->request->input('material_id',0);
        $assign_parent_id = $this->request->input('assign_parent_id',0);
        $title = $this->request->input('title','');
        $district_id = $this->request->input('district_id',0);
        $street_id = $this->request->input('street_id',0);
        $community_id = $this->request->input('community_id',0);
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_TWO
            ]
        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ],
            'material_sheet_assign' => [
                'on' => ['material_sheet_assign.id','=','material_flow.material_assign_id'],
                'where'=>[]
            ]
        ];

        if($status!='all'){
            $search['status']=[
                'field' => 'material_flow.status',
                'value' =>$status
            ];
        }
        if($title!=''){
            $search['title'] =[
                'field' => 'title',
                'value' => $title,
                'method' => 'like'
            ];
        }
        if($district_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $district_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_ONE
            ];
        }
        if($street_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $street_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_TWO
            ];
        }
        if($community_id){
            $search['data_id']=[
                'field' => 'data_id',
                'value' => $community_id
            ];
            $search['type']=[
                'field' => 'type',
                'value' => MaterialSheetAssign::TYPE_THREE
            ];
        }

        if($assign_parent_id){
            $search['assign_parent_id'] = [
                'field' => 'material_flow.assign_parent_id',
                'value' => $assign_parent_id
            ];
        }

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            $project_ids =[];
            $material_ids=[];
            $sheet_ids = [];
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $assign_from= MaterialSheetAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
                $assign = $assign_from['assign'];
                $item['assign'] = $assign_from['assign'];
                $item['report_from'] = $assign_from['from'];
                $item['contact'] = $assign_from['contact'];
                if(isset($project_ids[$assign->project_id])){
                    $item['project'] = $project_ids[$assign->project_id];
                }else{
                    $project = MaterialProjectRepository::getInstance()->one(['id','name'],[[
                        'field' => 'id',
                        'value' => $assign->project_id
                    ]]);
                    $project_ids[$assign->project_id] = $project;
                    $item['project'] = $project;
                }
                if(isset($material_ids[$assign->material_id])){
                    $item['material'] = $material_ids[$assign->material_id];
                }else{
                    $material = MaterialRepository::getInstance()->one(['id','title','start_time','end_time'],[[
                        'field' => 'id',
                        'value' => $assign->material_id
                    ]]);
                    $material_ids[$assign->material_id] = $material;
                    $item['material'] = $material;
                }

                if(isset($sheet_ids[$assign->sheet_id])){
                    $item['sheet'] = $sheet_ids[$assign->sheet_id];
                }else{
                    $sheet = MaterialProjectSheetRepository::getInstance()->one(['id','sheet_name'],[[
                        'field' => 'id',
                        'value' => $assign->sheet_id
                    ]]);
                    $sheet_ids[$assign->sheet_id] = $sheet;
                    $item['sheet'] = $sheet;
                }

            }
        }
        return $this->response->success($result);
    }

    public function scoreListbak(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $self_admin = $this->admin_service->getFlowSelf($admin);
        $columns=[
            'material_flow.*',
            'material.title',
        ];
        $search=[
            'flow_type' => [
                'field' => 'flow_type',
                'value' => MaterialFlow::FLOW_TYPE_TWO
            ]
        ];
        $join = [
            'material' => [
                'on' => ['material.id','=','material_flow.material_id'],
                'where'=>[]
            ]
        ];

        $orderBy= [
            [
                'field' => 'material_flow.created_at',
                'sort' => 'desc'
            ]
        ];
        $result = MaterialFlowRepository::getInstance()->getList($columns,$search,['limit'=>$rows],$orderBy,$join);
        if(!empty($result['result'])){
            foreach ($result['result'] as &$item){
                $flow = json_decode(collect($item)->toJson());
                $item['from'] = MaterialFlowRepository::getInstance()->getFromData($flow);
                $item['report_from'] = MaterialAssignRepository::getInstance()->getReportFrom($item['material_assign_id']);
            }
        }
        return $this->response->success($result);
    }

    public function areas(){
        $admin = $this->request->getAttribute('admin');
        if($admin->community_id){
            return $this->response->success([
                'areas' => [],
                'next' => null
            ]);
        }
        if($admin->street_id){
            $lists = CommunityRepository::getInstance()->all(['id','name','street_id'],[[
                'field' => 'street_id',
                'value' => $admin->street_id
            ]]);

            return $this->response->success([
                'areas' => $lists,
                'next' => null
            ]);
        }
        if($admin->district_id){
            $lists = StreetRepository::getInstance()->all(['id','name','district_id'],[[
                'field' => 'district_id',
                'value' => $admin->district_id
            ]]);

            return $this->response->success([
                'areas' => $lists,
                'next' => 'community'
            ]);
        }

        $lists = AreaRepository::getInstance()->all(['id','name'],[[
            'field' => 'parent_id',
            'value' => 223
        ]]);
        return $this->response->success([
            'areas' => $lists,
            'next' => 'street'
        ]);

    }
}